﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Hyunhdai_Saigon_Project.uc
{
    public partial class uc_admin_sp_timkiem : System.Web.UI.UserControl
    {
        public PagedDataSource pds = new PagedDataSource();
        public string sql;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                int inxs = Convert.ToInt16(Request.QueryString["nsx"]);

                int idongxe = Convert.ToInt16(Request.QueryString.Get("dgx"));
                string strdongxe = "0";        
                if(idongxe ==1)
                    strdongxe="Xe Du Lịch";
                else if (idongxe == 2)
                    strdongxe = "Xe Khách";
                else if(idongxe ==3)
                    strdongxe = "Xe Tải";

                string strdx = Request.QueryString["dx"];
                string strnt = Request.QueryString["nt"];
                string strsc = Request.QueryString["sc"];
                string strscn = Request.QueryString["scn"];
                string strhs = Request.QueryString["hs"];
                string strnl = Request.QueryString["nl"];
                string strhtnl = Request.QueryString["htnl"];
                sql = "select * from xe";

                if (inxs!=0)
                {
                    sql += " where idnsx =" + inxs;
               }

                if (!strdongxe.Equals("0"))
                {
                    if (sql.Contains("where"))
                        sql += " and dongxe = '" + strdongxe + "'";
                    else
                        sql += " where dongxe = '" + strdongxe + "'";
                }
                if (!strdx.Equals("none"))
                {
                    if (sql.Contains("where"))
                        sql += " and doixe = " + strdx;
                    else
                        sql += " where doixe = " + strdx;
                }

                if (!strnt.Equals("none"))
                {
                    if (sql.Contains("where"))
                        sql += " and ngoaithat = '" + strnt+"'";
                    else
                        sql += " where ngoaithat = '" + strnt+"'";
                }
                if (!strsc.Equals("none"))
                {
                    if (sql.Contains("where"))
                    sql += " and socua = " + strsc;
                    else
                     sql += " where socua = " + strsc;
                }
                if (!strscn.Equals("none"))
                {
                    if (sql.Contains("where"))
                    sql += " and sochongoi = " + strscn;
                    else
                        sql += " where sochongoi = " + strscn;
                }
                if (!strhs.Equals("none"))
                {
                    if (sql.Contains("where"))
                    sql += " and hopso = '" + strhs + "'";
                    else
                        sql += " where hopso = '" + strhs + "'";
                }
                if (!strnl.Equals("none"))
                {
                    if (sql.Contains("where"))
                    sql += " and nhienlieu = '" + strnl + "'";
                    else
                        sql += " where nhienlieu = '" + strnl + "'";
                }
                if (!strhtnl.Equals("none"))
                {
                    if (sql.Contains("where"))
                    sql += " and htnhienlieu = '" + strhtnl + "'";
                    else
                        sql += " where htnhienlieu = '" + strhtnl + "'";
                }

               if(!IsPostBack)
                manualBindGrid(sql);

            }
            catch (Exception)
            {
                if(!IsPostBack)
                BindGridHyundai();
            }
        }

        public void manualBindGrid(String sql)
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisg"].ConnectionString);
            connection.Open();
            SqlDataAdapter da = new SqlDataAdapter(sql, connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            connection.Close();
            pds.DataSource = dt.DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
            pds.CurrentPageIndex = CurrentPage;
            lnkbtnNext.Enabled = !pds.IsLastPage;
            lnkbtnPrevious.Enabled = !pds.IsFirstPage;
            dlCountry.DataSource = pds;
            dlCountry.DataBind();
            doPaging();
        }

        public void BindGridHyundai()
        {
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["hyundaisg"].ConnectionString);
            connection.Open();
            string sql = "select * from xe";
            SqlDataAdapter da = new SqlDataAdapter(sql, connection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            connection.Close();
            pds.DataSource = dt.DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
            pds.CurrentPageIndex = CurrentPage;
            lnkbtnNext.Enabled = !pds.IsLastPage;
            lnkbtnPrevious.Enabled = !pds.IsFirstPage;
            dlCountry.DataSource = pds;
            dlCountry.DataBind();
            doPaging();

        }

        public int CurrentPage
        {
            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 0;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }
            set
            {
                this.ViewState["CurrentPage"] = value;
            }

        }


        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            for (int i = 0; i < pds.PageCount; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }

            dlPaging.DataSource = dt;
            dlPaging.DataBind();

        }

        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("lnkbtnPaging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                manualBindGrid(sql);
            }
        }
        protected void lnkbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            manualBindGrid(sql);
        }

        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            manualBindGrid(sql);
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentPage = 0;
            manualBindGrid(sql);
        }

        protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Font.Bold = true;
            }
        }
        public String addchar(String str)
        {
            return "/" + str;
        }


        protected void click(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("click1"))
            {
                Label mx = (Label)e.Item.FindControl("mx");
                Response.Redirect("Cap-Nhap-San-Pham.aspx?p=" + mx.Text);
            }
        }
        protected void ItemDataBound(object sender, DataListItemEventArgs e)
        {
            Image la2 = (Image)e.Item.FindControl("text");
            Label dgx = (Label)e.Item.FindControl("dgx");
            Label dx = (Label)e.Item.FindControl("dx");
            Label nt = (Label)e.Item.FindControl("nt");
            Label sc = (Label)e.Item.FindControl("sc");
            Label scn = (Label)e.Item.FindControl("scn");
            Label hs = (Label)e.Item.FindControl("hs");
            Label nl = (Label)e.Item.FindControl("nl");
            Label htnl = (Label)e.Item.FindControl("htnl");
            Label ttnl = (Label)e.Item.FindControl("ttnl");

            string demo = "<table BORDER=1 CELLPADDING=3 CELLSPACING=1 RULES=ROWS RULES=COLS FRAME=BOX  class = tooltip>" + "<tr><td width = 180px>Dòng Xe:</td><td width:150px>" + dgx.Text + "</td></tr><tr><td>Đời Xe:</td><td>" + dx.Text + "</td></tr><tr><td>Ngoại Thất:</td><td>" + nt.Text + "</td></tr><tr><td>Số Cửa:</td><td>" + sc.Text + "</td></tr><tr><td>Số Chỗ Ngồi:</td><td>" + scn.Text + "</td></tr><tr><td>Hộp Số:</td><td>" + hs.Text + "</td></tr><tr><td>Nhiêu Liệu:</td><td>" + nl.Text + "</td></tr><tr><td>Hệ Thống Nhiên Liệu:</td><td>" + htnl.Text + "</td></tr><tr><td>Tiêu Thụ Nhiên Liệu:</td><td>" + ttnl.Text + "</td></tr></table>";


            String str = dgx.Text + "<br />" + dx.Text + "<br />" + nt.Text + "<br />" + sc.Text + "<br />" + scn.Text + "<br />" + hs.Text + "<br />" + nl.Text + "<br />" + htnl.Text + "<br />" + ttnl.Text;
            la2.Attributes.Add("onMouseover", "ddrivetip(' " + demo + " ', 298,'white');");
            la2.Attributes.Add("onmouseout", "hideddrivetip();");

        }
     
    }
}